package com.ldc.commonres.uts

import android.content.Context
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.annotation.Nullable
import androidx.annotation.RawRes
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.ldc.commonres.R
import java.io.File

object GlideUts {
    fun getRequestOption(): RequestOptions = getRequestOption(R.drawable.res_icon_placeholder)

    private fun getRequestOption(defImage: Int): RequestOptions = RequestOptions()
        .placeholder(defImage)
        .diskCacheStrategy(DiskCacheStrategy.NONE)
        .error(R.drawable.res_icon_placeholder)

    fun setImage(context: Context, url: String, imageView: ImageView, defImage: Int) {
        Glide.with(context)
            .load(url)
            .apply(getRequestOption(defImage))
            .into(imageView)
    }

    fun setImage(context: Context, url: String, imageView: ImageView) {
        setImage(context, url, imageView, R.drawable.res_icon_placeholder)
    }

    private fun setImage(context: Context, url: Int, imageView: ImageView, defImage: Int) {
        Glide.with(context)
            .load(url)
            .apply(getRequestOption(defImage))
            .into(imageView)
    }

    fun setImage(context: Context, url: Int, imageView: ImageView) {
        setImage(context, url, imageView, R.drawable.res_icon_placeholder)
    }

    fun setImage(context: Context, file: File, imageView: ImageView) {
        setImage(context, file, imageView, R.drawable.res_icon_placeholder)
    }

    fun setImage(context: Context, file: File, imageView: ImageView, defImage: Int) {
        Glide.with(context)
            .load(file)
            .apply(getRequestOption(defImage))
            .into(imageView)
    }


    /**
     * 普通设置图片（含默认图片）
     *
     * @param context
     * @param url
     * @param imageView
     */
    fun setImageFitCenter(context: Context, url: String, imageView: ImageView, radius: Float) {

        if (imageView.scaleType != ImageView.ScaleType.FIT_XY) {
            imageView.scaleType = ImageView.ScaleType.FIT_XY
        }

        Glide.with(context)
            .asBitmap()
            .load(url)
            .apply(getFitCenterRequestOption(radius))
            .into(imageView)
    }

    /**
     * 普通设置图片（含默认图片）
     *
     * @param context
     * @param url
     * @param imageView
     */
    fun setImageFitCenter(
        context: Context?,
        @RawRes @DrawableRes @Nullable url: Int?,
        imageView: ImageView,
        radius: Float,
    ) {
        if (imageView.scaleType != ImageView.ScaleType.FIT_XY) {
            imageView.scaleType = ImageView.ScaleType.FIT_XY
        }
        Glide.with(context!!)
            .asBitmap()
            .load(url)
            .apply(getFitCenterRequestOption(radius))
            .into(imageView)
    }

    fun setImageFitCenter(context: Context, url: String, imageView: ImageView) {
        setImageFitCenter(context, url, imageView, 2F)
    }

    /**
     * 普通设置图片（含默认图片）
     *
     * @param context
     * @param url
     * @param imageView
     */
    fun setImageCenterCrop(context: Context?, url: String?, imageView: ImageView, radius: Int) {
        if (imageView.scaleType != ImageView.ScaleType.CENTER_CROP) {
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        }

        Glide.with(context!!)
            .asBitmap()
            .load(url)
            .apply(getCenterCropRequestOption(radius))
            .into(imageView)
    }

    /**
     * 普通设置图片（含默认图片）
     *
     * @param context
     * @param url
     * @param imageView
     */
    fun setImageCenterCrop(context: Context?, url: Int?, imageView: ImageView, radius: Int) {
        if (imageView.scaleType != ImageView.ScaleType.CENTER_CROP) {
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        }

        Glide.with(context!!)
            .asBitmap()
            .load(url)
            .apply(getCenterCropRequestOption(radius))
            .into(imageView)
    }

    /**
     * 普通设置图片（含默认图片）
     *
     * @param context
     * @param url
     * @param imageView
     */
    fun setImageCenterCrop(
        context: Context?,
        @RawRes @DrawableRes url: Int?,
        imageView: ImageView,
        radius: Float,
    ) {
        if (imageView.scaleType != ImageView.ScaleType.CENTER_CROP) {
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        }
        Glide.with(context!!)
            .asBitmap()
            .load(url)
            .apply(getFitCenterRequestOption(radius))
            .into(imageView)
    }

    fun setImageCenterCrop(context: Context?, url: String?, imageView: ImageView) {
        setImageCenterCrop(context, url, imageView, 2)
    }

    fun setImageCenterCrop(context: Context?, url: Int?, imageView: ImageView) {
        setImageCenterCrop(context, url, imageView, 2)
    }

    fun getFitCenterRequestOption(radius: Float) = RequestOptions
        .fitCenterTransform()
        .transform(RoundedCorners(8))
        .placeholder(R.drawable.res_icon_placeholder)
        .placeholder(R.drawable.res_icon_placeholder)
        .skipMemoryCache(false)
        .dontAnimate()
        .diskCacheStrategy(DiskCacheStrategy.ALL)

    fun getCenterCropRequestOption(radius: Int): RequestOptions = RequestOptions()
        .placeholder(R.drawable.res_icon_placeholder)
        .placeholder(R.drawable.res_icon_placeholder)
        .skipMemoryCache(false)
        .dontAnimate()
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .transforms(CenterCrop(), RoundedCorners(radius))

}